<template>
  <a-modal
    :title="modalTitle"
    :width="1100"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" style="max-height: 500px;overflow-y: auto;overflow-x: hidden;">
        <a-form-item label="业务ID" :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="false">
          <a-input v-if="modalOpera==1" v-decorator="['id', {rules: [{required: true}]}]" />
        </a-form-item>

        <p>
          分类扩展名称
          <a-icon type="right" />
        </p>
        <a-row :gutter="6">
          <a-col :xl="{span: 8}" :lg="{span: 8}" :md="{span: 12}">
            <a-form-item label="模型名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                v-decorator="['categoryModelName', {rules: [{required: true, min: 2, message: '请输入至少二个字符的规则！'}]}]"
              />
            </a-form-item>
          </a-col>

          <a-col :xl="{span: 8}" :lg="{span: 8}" :md="{span: 12}">
            <a-form-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number
                style="width:100%"
                :min="0"
                v-decorator="['sort', {rules: [{required: false, message: '请输入至少二个字符的规则！'}],initialValue:0}]"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-divider dashed />
        <extend-model ref="extendModel" />
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { save, update } from '@/api/site/categoryModel'
import pick from 'lodash.pick'
import ExtendModel from '@/components/ExtendModel/ExtendModel.vue'
export default {
  components: {
    ExtendModel
  },
  data () {
    return {
      labelCol: {
        xs: {
          span: 24
        },
        sm: {
          span: 7
        }
      },
      wrapperCol: {
        xs: {
          span: 24
        },
        sm: {
          span: 13
        }
      },
      modalTitle: '',
      modalOpera: 0, // 0：新增 1：编辑
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this)
    }
  },
  created () {},
  mounted () {},
  methods: {
    add () {
      this.modal('新建分类扩展', 0)
    },
    edit (record) {
      const that = this
      this.modal('编辑分类扩展', 1)
      setTimeout(function () {
        that.getDetail(record)
      }, 100)
    },
    getDetail (result) {
      this.form.setFieldsValue(pick(result, 'id', 'categoryModelName', 'sort'))
      this.setDataSource(JSON.parse(result.extendFieldList))
    },
    modal (title, opera) {
      this.modalTitle = title
      this.modalOpera = opera
      this.visible = true
      this.form.resetFields()
    },
    handleSubmit () {
      const {
        form: { validateFields }
      } = this
      validateFields((errors, values) => {
        if (!errors) {
          this.confirmLoading = true
          const fields = this.getDataSource()
          if (fields.length === 0) {
             this.$message.error('您还没有创建字段,请先创建字段后保存!')
             this.confirmLoading = false
             return
          }
          values.extendFieldList = JSON.stringify(fields)
          if (this.modalOpera === 0) {
            save(values).then(response => {
               this.message(response)
            })
          } else if (this.modalOpera === 1) {
            update(values).then(response => {
               this.message(response)
            })
          }
        }
        this.confirmLoading = false
      })
    },
    message (response) {
      if (response.code === 0) {
        this.$message.info(response.msg)
        this.$emit('ok')
        this.handleCancel()
      } else {
        this.$message.error(response.msg)
      }
    },
    handleCancel () {
      this.visible = false
      this.confirmLoading = false
      this.clearDataSource()
    },
    setDataSource (extendFieldList) {
       if (extendFieldList) {
          this.$refs.extendModel.setDataSource(extendFieldList)
       } else {
          this.$refs.extendModel.setDataSource([])
       }
    },
    getDataSource () {
      return this.$refs.extendModel.getDataSource()
    },
    clearDataSource () {
      this.$refs.extendModel.clearDataSource()
    }
  }
}
</script>
